package com.example.bai8;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	EditText edtcv;
	EditText edtgl;
	EditText edtnd;
	EditText edtngl;
	
	Button btnadd;
	Button btndate;
	Button btntime;
	
	ListView list;
	ArrayList<JobInWeek> arr;
	ArrayAdapter<JobInWeek> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getControlWidgets();
        setDefaultValue();
        addEventsForWidget();
    }

    private void getControlWidgets()
    {
    	 edtcv=(EditText) findViewById(R.id.edtcv);
    	 edtgl=(EditText) findViewById(R.id.edtgl);
    	 edtnd=(EditText) findViewById(R.id.edtnd);
    	 edtngl=(EditText) findViewById(R.id.edtngl);
    	
    	 btnadd=(Button) findViewById(R.id.btnadd);
    	 btndate=(Button) findViewById(R.id.btndate);
    	 btntime=(Button) findViewById(R.id.btntime);
    	
    	 list=(ListView) findViewById(R.id.list);
    	 arr= new ArrayList<JobInWeek>();
    	 adapter= new ArrayAdapter<JobInWeek>(this,android.R.layout.simple_list_item_1,arr);
    	 list.setAdapter(adapter);
    	 
    }
    private void setDefaultValue()
    {
    	Calendar cal= Calendar.getInstance();
    	SimpleDateFormat dft= new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
    	edtngl.setText(dft.format(cal.getTime()));
    	dft= new SimpleDateFormat("hh:mm a",Locale.getDefault());
    	edtgl.setText(dft.format(cal.getTime()));
    	
    	dft= new SimpleDateFormat("HH:mm",Locale.getDefault());
    	edtgl.setTag(dft.format(cal.getTime()));
    }
    
    private void addEventsForWidget()
    {
    	btnadd.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try
				{
				String tilte= edtcv.getText().toString();
				String content=edtnd.getText().toString();
				SimpleDateFormat dft= new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
				Date date=(Date) dft.parse(edtngl.getText().toString());
				
				dft=new SimpleDateFormat("HH:mm",Locale.getDefault());
				Date time=(Date) dft.parse(edtgl.getText().toString());
				JobInWeek job= new JobInWeek(tilte,content,date,time);
				
				arr.add(job);
				adapter.notifyDataSetChanged();
				}
				catch(Exception ex)
				{
					Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
				}
				edtcv.setText("");
				edtnd.setText("");
			}
		});
    	btndate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final OnDateSetListener callback= new OnDateSetListener() {
					
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						// TODO Auto-generated method stub
						edtngl.setText(dayOfMonth+"/"+monthOfYear+"/"+year);
						
					}
					
						};
						Calendar cal= Calendar.getInstance();
						SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
						String date=sdf.format(cal.getTime());
						String[] split=date.split("/");
						int day=Integer.parseInt(split[0]);
						int month=Integer.parseInt(split[1]);
						int year=Integer.parseInt(split[2]);
						DatePickerDialog dpd= new DatePickerDialog(MainActivity.this,callback,year, month,day);
				dpd.setTitle("Date");
				dpd.show();
			}
		});
    	
    	btntime.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				OnTimeSetListener callback= new OnTimeSetListener() {
					
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						// TODO Auto-generated method stub
						edtgl.setText(hourOfDay+":"+minute);
						
					}
				};
				
				Calendar cal= Calendar.getInstance();
				SimpleDateFormat sdf= new SimpleDateFormat("HH:mm",Locale.getDefault());
				String time= sdf.format(cal.getTime());
				String[] split=time.split(":");
				int hour=Integer.valueOf(split[0].trim());
				int min=Integer.valueOf(split[1].trim());
				
				TimePickerDialog tpd= new TimePickerDialog(MainActivity.this, callback, hour, min,true );
				tpd.setTitle("Time");
				tpd.show();
			}
		});
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
